import { creepNameToObj, objIDToObj } from "../../../tools/baseTools"
import { updateCostMat } from "../../../tools/memoryData"
import { changeCreepRule, creepGetSource, creepIsNotRuning, creepMoveExtesion } from "../../../tools/tools/creepTool"
import taskManager from "../../task/taskManager"

export default {
    run: function (creep) {
        creep = creepNameToObj(creep)
        // 每次上传代码，creep内存初始化会删除task，因此会删除currentTaskID
        if (!creep.memory.currentTaskID) changeCreepRule(creep) // 每一个creep都需要存在一个无currentTaskID的情况
        if (creep.memory.state.isWorking) {
            // 工作逻辑
            ruleBuilderWorking(creep)
        } else {
            // 准备逻辑
            creepGetSource({ creep: creep, room: creep.room, sourceType: creep.memory.params.resouce })
        }
    }
}

function ruleBuilderWorking(creep) {
    creep = creepNameToObj(creep)
    let targetObj = objIDToObj(creep.memory.targetID[creep.memory.targetID.length - 1])
    if (targetObj) {
        if (!creep.memory.params.pos) creep.memory.params.pos = targetObj.pos
        if (!creep.memory.params.structureType) creep.memory.params.structureType = targetObj.structureType
    }
    let resultCode = creep.build(targetObj)
    // console.log(creep.name+`执行builder的逻辑结果编码${resultCode}`);
    switch (resultCode) {
        case OK:
            if (creep.memory.state.isRuning) {
                creepIsNotRuning(creep)
            }
            break;
        case ERR_NOT_IN_RANGE:
            creepMoveExtesion(creep)
            break;
        case ERR_NOT_ENOUGH_RESOURCES:
            creepIsNotRuning(creep)
            creep.memory.state.isWorking = false
            break;
        case ERR_INVALID_TARGET:
            if (creep.memory.params.pos && creep.memory.params.structureType) {
                // 更新已完成的建筑
                newBuildUpdata(creep.memory.params.pos, creep.memory.params.structureType)
                console.log(`${creep.name}完成在${JSON.stringify(creep.memory.params.pos)}位置的${creep.memory.params.structureType}类型建筑`);
            }
            creep.memory.targetID.pop()
            creep.memory.params.pos = null
            creep.memory.params.structureType = null
            // 最终结束流程，切换回freeman
            if (creep.memory.targetID.length == 0) {
                taskManager.finishTask(creep.memory.currentTaskID, creep.memory.targetPos.roomName, { obj: creep })
                changeCreepRule(creep)
                console.log(`${creep.name}的建筑任务已经完成,切换回freeman角色重新获取任务`);
                return
            }
            break;
        default:
            creepIsNotRuning(creep)
            console.log(creep.name + `使用builder的逻辑可能出现问题了,错误编码${resultCode}`);
            break;
    }

}
/**新增建筑需要更新建筑在各处的数据
 * 1. 寻路中的更新
 * 2. 房间内存中Structure中的更新
 */
function newBuildUpdata(pos, structureType) {
    let newBuild = Game.rooms[pos.roomName].lookAt(new RoomPosition(pos.x, pos.y, pos.roomName))
    newBuild = newBuild.filter(buildList => buildList.type == 'structure').filter(build => {
        if (build.structure.structureType == structureType) return true
    })
    if (newBuild.length != 0) {
        newBuild = newBuild[0].structure
        updateCostMat(pos.roomName, { pos: pos, structureType: structureType })
        if (!Memory.rooms[pos.roomName]['structure'][structureType]) Memory.rooms[pos.roomName]['structure'][structureType] = {}
        Memory.rooms[pos.roomName]['structure'][structureType][newBuild.id] = {
            pos: newBuild.pos,
            publishTask: [],
            isRuning: false,
            creeps: [],
        }
        // console.log('完成内存的添加'+JSON.stringify(Memory.rooms[pos.roomName]['structure'][structureType][newBuild.id]));
    }
}